﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;


public partial class kecheng_search1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["sUserName"] == null)
        {

            Response.Write("<script language=javascript>alert('未登录或登录已到期，请返回登录页面重新登录！');if(opener!=null){opener.location.reload();window.close();}</script>");
            Response.Write("<script language=javascript> window.open('../logout.aspx',target='_top')</script>");
            return;
        }
        Session["sTittle"] = "查询课程";

    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }
        //以下，当课程已经报名或已经通过时，字体显示粉色
        //相关session变量：Session["sKecheng"]，Session["sKechenged"]
        //keydata技巧：GridView1.DataKeys[4]["laoshi"].ToString()
        if (Session["sRole"].ToString() == "学员" & e.Row.RowIndex != -1)
        {
            string zid = "," + GridView1.DataKeys[e.Row.RowIndex]["id"].ToString() + ",";
            if (Session["sKecheng"].ToString().IndexOf(zid) > 0 | Session["sKechenged"].ToString().IndexOf(zid) > 0)
            {
                e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
            }
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        if (e.NewSelectedIndex >= 0)
        {
            string zsql = "SELECT * from kecheng where id=" + GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
            SqlDataSource2.SelectCommand = zsql;
        }
        else
        {
            DetailsView1.Visible = false;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Conn zconn = new Conn();
        int j = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            j = j++;
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {

                string zid = GridView1.Rows[i].Cells[3].Text;         //e.Row.Cells[0].Text
                //在删除课程之前,检查欲删除的课程是否已被学生许修
                //select kecheng,kechenged from account where kecheng like '%,10,%' or kechenged like '%10%'
                string zsql = "select username from account where kecheng like '%," + zid + ",%' or kechenged like '%" + zid + "%'";
                string noth = zconn.sqlFirst(zsql);
                if (noth != "0")
                {
                    Response.Write("<script language=javascript>alert('本课程已被学生选修,删除课程不成功．');if(opener!=null){opener.location.reload();window.close();}</script>");

                }
                else      //删除
                {
                    zsql = "delete from kecheng where id=" + zid;
                    string rsql = "select topic from kecheng where id=" + zid;
                    string bbsid = zconn.sqlFirst(rsql);

                    if (zconn.ExeSql(zsql) == 1)
                    {
                        //以下由RH编写，在课程删除之后，把课程对应的论坛信息都删除.
                        string rsql2 = "select f_filename,img_id,bbs.title_id from bbs,imgupload where bbs.title_id=imgupload.title_id and f_topic like '%" + bbsid + "%'";
                        SqlConnection MyConnection = new SqlConnection(zconn.strProvider);
                        SqlCommand myCommand = new SqlCommand(rsql2, MyConnection);
                        MyConnection.Open();
                        SqlDataReader reader1 = myCommand.ExecuteReader();
                        while (reader1.Read())
                        {
                            //删除文件
                            string removefilename = reader1.GetValue(0).ToString();
                            removefilename = Server.MapPath("../bbs/uploadimage") + "/" + removefilename;
                            File.Delete(removefilename);

                            //删除imgupload表的记录
                            string imgid = reader1.GetValue(1).ToString();
                            string rsql3 = "delete imgupload where img_id="+imgid;
                            zconn.ExeSql(rsql3);

                            //删除bbs表的记录
                            string titleid = reader1.GetValue(2).ToString();
                            string rsql4 = "delete bbs where title_id=" + titleid;
                            zconn.ExeSql(rsql4);
                        }
                        reader1.Close();
                        MyConnection.Close();

                        //删除bbsconfig表的记录
                        string rsql5 = "delete bbsconfig where bbs_id=" + bbsid;
                        zconn.ExeSql(rsql5);


                        //*********************************************************

                        Response.Write("<script language=javascript>alert('删除课程成功．');if(opener!=null){opener.location.reload();window.close();}</script>");

                    }
                    else
                    {

                        Response.Write("<script language=javascript>alert('删除课程不成功．');if(opener!=null){opener.location.reload();window.close();}</script>");
                    }
                }

            }

        }
        zconn.Dispose();
        GridView1.DataBind();

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //string zconj = "([confirm]='终审通过')";  //对于管理员,可以查询任何状况的课程,无论是否通过
        string zconj = "(1=1')";
        if (DropDownList1.SelectedValue.Length > 0)
        {
            zconj += " AND ([fenlei1] ='" + DropDownList1.SelectedValue + "')";
        }
        if (DropDownList2.SelectedValue.Length > 0)
        {
            zconj += " AND ([fenlei2] ='" + DropDownList2.SelectedValue + "')";
        }
        if (DropDownList3.SelectedValue.Length > 0)
        {
            zconj += " AND ([fenlei3] ='" + DropDownList3.SelectedValue + "')";
        }
        if (DropDownList4.SelectedValue.Length > 0)
        {
            zconj += " AND ([kechenglx] ='" + DropDownList4.SelectedValue + "')";
        }


        SqlDataSource1.SelectCommand = "SELECT * FROM [kecheng] WHERE " + zconj;

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM [kecheng] WHERE [confirm]='终审通过'";
        GridView1.DataBind();
        DropDownList1.SelectedIndex = -1;
        DropDownList2.SelectedIndex = -1;
        DropDownList3.SelectedIndex = -1;
        DropDownList4.SelectedIndex = -1;

    }

    protected void Button4_Click(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //打开kecheng_play_temp.传递参数：

        //最全代码：Response.Write("<script language=javascript> window.open('../mystudy/kecheng_play.aspx','newwindow', 'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no')</script>");
        //本节应用代码：Response.Write("<script language=javascript> window.open('../mystudy/kecheng_play.aspx','newwindow', 'height=768,width=1024,top=0,left=0,toolbar=no,resizable=yes,menubar=no,location=no,status=no')</script>");
        //string zid=GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
        //string zname = GridView1.Rows[e.NewSelectedIndex].Cells[4].Text;
        if (e.CommandName.ToString() != "Select" & e.CommandName.ToString() != "Page" & e.CommandName.ToString() != "Sort")
        {
            string zid = e.CommandArgument.ToString();
            //能成功运行的命令--Response.Write("<script language=javascript> window.open('../mystudy/kecheng_play.aspx','newwindow', 'height=768,width=1024,top=0,left=0,toolbar=no,resizable=yes,menubar=no,location=no,status=no')</script>");

            //修改后（加入参数传递）不能运行Response.Write("<script language=javascript> window.open('../mystudy/kecheng_play_temp.aspx?rid='"+zid+",'kecheng_play_window', 'height=768,width=1024,top=0,left=0,toolbar=no,resizable=yes,menubar=no,location=no,status=no')</script>");
            string zscript = "<script language=javascript> window.open('../mystudy/kecheng_play_temp.aspx?rid=" + zid + "','kecheng_play_window', 'height=768,width=1024,top=0,left=0,toolbar=no,resizable=yes,menubar=no,location=no,status=no')</script>";
            Response.Write(zscript);
        }
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        //按照课程名称查询
        string noth = TextBox2.Text.Trim();
        SqlDataSource1.SelectCommand = "SELECT * FROM [kecheng] WHERE [confirm]='终审通过' AND [name] like '%" + noth + "%'";
    }
}
